var contextmenu = null;
/**
 * 鼠标右键控件
 * 通过读取xml文件，或者用户输入右键内容的字符串
 * @param container:显示右键菜单的容器
 */
ContextMenuDiv = function(container){
	this._container = container;
	this._menupanel = document.createElement("div");//菜单面板
	this._menupanel.id = "menupanel";
	this._menupanel.style.width = 90;
	//this._menupanel.style.height = auto;
	this._menupanel.style.position = "absolute";
	this._menupanel.style.border = "1px";
	this._menupanel.style.zIndex = "100"; 
	this._menupanel.style.background = "none repeat scroll 0 0 #FFFFFF";
	this._menupanel.style.borderColor = "#ADBFE4 #8BA4D8 #8BA4D8 #ADBFE4";
	this._menupanel.style.borderStyle = "solid";
	this._menupanel.style.borderWidth = "1px";
	this._menupanel.style.display = "none";
	this._posx = 0;
	this._posy = 0;
	this._menuDoc = null;//菜单的内容
	document.body.appendChild(this._menupanel);
	contextmenu = this; 
};
ContextMenuDiv.prototype = {
//--------------- public method start ---------------------------------------
	
	//设置菜单面板的宽度	 
	setWidth:function(w){
		contextmenu._menupanel.style.width = w;
	},
	
	//设置菜单面板的高度
	setHeight:function(h){
		contextmenu._menupanel.style.height = h;
	}, 
	
	showContextMenu:function(e){
		contextmenu.hideMenu();
	    if (!e) var e = window.event;
	    if (e.pageX || e.pageY){
	        contextmenu._posx = e.pageX;
	        contextmenu._posy = e.pageY;
	    }
	    if (e.clientX || e.clientY) {
	        contextmenu._posx = e.clientX + document.body.scrollLeft
	            + document.documentElement.scrollLeft;
	        contextmenu._posy = e.clientY + document.body.scrollTop
	            + document.documentElement.scrollTop;
	    }
	   
	    contextmenu.showMainDiv();
	    return false;
	},
	
	//显示主div层
	showMainDiv:function(){
	    var x,y;
	    contextmenu._menupanel.style.display = 'block';
	    if((contextmenu._posx + contextmenu._menupanel.offsetWidth)  >  document.body.clientWidth){
	        x = contextmenu._posx - contextmenu._menupanel.offsetWidth;
	        contextmenu._menupanel.style.left  = x + 3 + 'px';
	    }else{
	        contextmenu._menupanel.style.left  = contextmenu._posx + 3 + 'px';
	    }
	    if((contextmenu._posy + contextmenu._menupanel.offsetHeight) >  document.body.clientHeight){
	        y = contextmenu._posy - contextmenu._menupanel.offsetHeight;
	        contextmenu._menupanel.style.top   = y + 'px';
	    }else{
	        contextmenu._menupanel.style.top   = contextmenu._posy + 'px';
	    }
	},
	
	//隐藏memu面板
	hideMenu:function(){
    	contextmenu._menupanel.style.display='none';   	 	
	},
	
	//针对两种浏览器，分别获取xmlDocument对象  
	loadByXML:function(xmlfile){
        if(window.ActiveXObject) {   
            contextmenu._menuDoc=new ActiveXObject("Microsoft.XMLDOM");   
            contextmenu._menuDoc.async=false;  
            contextmenu._menuDoc.load(xmlfile);  
        }  
        else if(document.implementation&&document.implementation.createDocument){   
            contextmenu._menuDoc=document.implementation.createDocument( "", "",null);   
            contextmenu._menuDoc.async=false;  
            contextmenu._menuDoc.load(xmlfile);  
        } else{  
            alert('Your   browser   cannot   handle   this   script');   
        }    
        contextmenu._parseMenuDoc();
        contextmenu._container.oncontextmenu = contextmenu.showContextMenu;
		contextmenu._container.onclick = contextmenu.hideMenu;
    },
	
	loadByString:function(str){
	
	},
	
//--------------- public method end -----------------------------------------
//--------------- private method start --------------------------------------
	//解析菜单文档对象
	_parseMenuDoc:function(){ 
		var nodes = contextmenu._menuDoc.documentElement.childNodes;
		var cancel = '<div id="cancel" οnclick="contextmenu.hideMenu()" onMouseover = "this.style.backgroundColor=/'#ADBFE4/'" onMouseout = "this.style.backgroundColor =/'#ffffff/'">取消</div>';
			
		contextmenu._menupanel.innerHTML = contextmenu._menuDoc.xml+cancel;
		
		/*for(var i=0;i<nodes.length;i++){
			var node;
			try{
			 	node = nodes.nextNode();
			 	var menu = document.createElement("div");
				menu.id = node.getAttribute("id");
				menu.onclick = node.getAttribute('onclick');
				menu.style.zIndex = contextmenu._menupanel.style.zIndex+1;
				menu.innerHTML = node.nodeTypedValue;
				menu.style.backgroundColor='#131417';
				menu.style.cursor = "hand";
				menu.onMouseover = "this.style.backgroundColor='#101010'";
				menu.onMouseout = "this.style.backgroundColor ='#ffffff'";
				contextmenu._menupanel.appendChild(menu);
			}catch(e){
				node = nodes[2*i+1];
			}
			
		}*/
	} 
//--------------- private method end  ---------------------------------------
};
